<?php
namespace app\admin\controller;

use think\Request;

class Dingdan extends Common
{
    //订单列表首页
    public function index()
    {
        //降序排列,分页是一个对象
        $dingdanlist = db('dingdan')->paginate(5);

        $goodslist = db('dingdan_goods')->alias('a')->join('goods b','b.id= a.goods_id')->join('goods_attributes c','c.id= a.attributes_id')->join('goods_guigetype d','c.guigeType_id= d.id')->join('goods_guige f','f.id= d.guige_id')->field('a.*,b.name as goods_name,b.src as goods_src,c.price,d.name as guigetype_name,d.pid as guigetype_pid,d.guige_id,f.name as guige_name,f.pid as guige_pid')->select();
        //dump($goodslist);die;
        $page=$dingdanlist->render();
        $page = str_replace(
            ["pagination", 
            "disabled",
            "active"],
        ["am-pagination tpl-pagination",
        "am-disabled",
        "am-active"],
        $page);
        //用数据对象的形式进行访问
        $list=$dingdanlist->items();
        //dump($list);die;
        //$list = $shangpinlist; //想要的结果
        //把订单号变为键再重组数组
        foreach ($list as $k => $v) {
            $user = db('user')->where("id = {$v['user_id']}")->find();
            //dump($user);die;
            $list[$k]['user_name']=$user['name'];
            foreach($goodslist as $k1 => $v1){
                if($v1['dingdan_id']!='' && $v1['dingdan_id']==$v['id']){
                    if($v1['guigetype_pid']!=0){
                        $ret=db('goods_guigetype')->where("id={$v1['guigetype_pid']}")->find();
                        $v1['guigetype1_name']=$ret['name'];
                        $v1['guigetype1_pid']=$ret['pid'];
                    }
                    if($v1['guige_pid']!=0){
                        $ret1=db('goods_guige')->where("id={$v1['guige_pid']}")->find();
                        $v1['guige1_name']=$ret1['name'];
                        $v1['guige1_pid']=$ret1['pid'];
                    }

                    $list[$k]['son'][]=$v1;
                }
            }
            
        }
        //dump($list);die;
        $this->assign('list',$list);
        $this->assign('page',$page);
        return view();
    }
    //发货页面
    public function fahuo()
    {
        $id=input('get.id');
        $address_id=input('get.address_id');
        //dump($id);die;
        $address=db('user_address')->where("id={$address_id}")->find();
        //dump($address);die;
        $this->assign('id',$id);
        $this->assign('address',$address);
        return view();    }

    //添加单号
    public function add_wuliu()
    {
        $data=input('post.');

        $id=input('get.id');
        $address_id=input('get.address_id');
        foreach($data as $key=>$val){
            $data['dingdan_id']=$id;
            $data['address_id']=$address_id;
            unset($data['shouhuo_address']);
            unset($data['shouhuo_tell']);
            unset($data['shouhuo_name']);
        }
        //dump($data);die;
        $validate = validate('Wuliu');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
         $ret=db('wuliu')->insert($data);
         if($ret){
            $result=db('dingdan')->where("id=$id")->setField('fahuo_status', 1);
            $this->success('发货成功','admin/dingdan/index');
         }

    }
    //物流详情
    public function wuliumore(){
        $dingdan_id=input('get.id');
        $address_id=input('get.address_id');
        $address=db('user_address')->where("id={$address_id}")->find();
        //dump($result);die;
        $ret=db('wuliu')->where("dingdan_id ='$dingdan_id'")->find();
        if($ret){
            $this->assign('ret',$ret);
            $this->assign('address',$address);
            return view();
        }else{
            $this->error('无此订单');
        }
        
    }
    //添加备注页面
    public function add_beizhu(){
        $dingdan_id=input('get.id');
        $this->assign('id',$dingdan_id);
        return view();
    }

    public function tianjia_beizhu(){
        $dingdan_id=input('get.id');
        $data=input('post.');
        if($data['dingdan_beizhu']==''){
            $this->error('备注不能为空');
        }
        $ret=db('dingdan')->where("id = {$dingdan_id}")->update($data);
        if($ret){
            $this->success('添加备注成功','admin/dingdan/index');

        }else{
            $this->error('添加备注失败');
        }

    }

    //编辑备注页面
    public function edit_beizhu(){
        $dingdan_id=input('get.id');
        $dingdan=db('dingdan')->where("id = {$dingdan_id}")->find();
        $this->assign('dingdan',$dingdan);
        return view();
    }

    //更新编辑
    public function update_beizhu(){
        $dingdan_id=input('get.id');
        $data=input('post.');
        if($data['dingdan_beizhu']==''){
            $this->error('备注不能为空');
        }
        $ret=db('dingdan')->where("id = {$dingdan_id}")->update($data);
        if($ret){
            $this->success('编辑备注成功','admin/dingdan/index');

        }else{
            $this->error('编辑备注失败');
        }
    }

}
